package pers.cz.controller;

import pers.cz.exception.ErrorCode;
import pers.cz.exception.BusinessException;
import pers.cz.model.SystemJobLog;
import pers.cz.model.TaskJobReport;
import pers.cz.pub.RespBean;
import pers.cz.service.TaskJobHisService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @program: PostGirl-panent
 * @description: TaskJobHisController
 * @author: Cheng Zhi
 * @create: 2021-10-31 14:39
 **/
@Api(tags = "定时任务历史实例接口")
@RestController
@RequestMapping(path = "/api/taskJobHis")
public class TaskJobHisController {

    @Autowired
    TaskJobHisService service;

    @ApiOperation(value = "分页查询所有实例")
    @PostMapping(path = "/queryAllJobLogByPage")
    public List<SystemJobLog> queryAllJobLogByPage(@RequestBody Map map) {

        if (map.get("pageStart") == null || map.get("pageLimit") == null) {
            throw new BusinessException(ErrorCode.REQUEST_IS_NULL_ERROR);
        }
        int pageStart = (int) map.get("pageStart");
        int pageLimit = (int) map.get("pageLimit");
        String jobId = (String) map.get("jobId");

        return service.queryAllJobLogByPage(Integer.valueOf(jobId), pageStart, pageLimit);

    }

    @ApiOperation(value = "查询总数用于分页")
    @PostMapping(path = "/queryJobLogCountNum")
    public Integer queryJobLogCountNum(@RequestBody Map map) {

        String jobId = (String) map.get("jobId");
        Integer count = service.queryJobLogCountNum(Integer.valueOf(jobId));
        return count;
    }

    @ApiOperation(value = "根据soNbr查询测试报告名称")
    @PostMapping(path = "/queryReporyNameBySonbr")
    public TaskJobReport queryReporyNameBySonbr(@RequestBody Map map) {

        Integer soNbr = (Integer) map.get("soNbr");
        return service.queryReporyNameBySonbr(Long.valueOf(soNbr));
    }

    @ApiOperation(value = "删除历史任务")
    @PostMapping(path = "/deleteJobLog")
    public RespBean deleteJobLog(@RequestBody Map map) {
        Integer id = (Integer) map.get("id");
        Integer count = service.deleteJobLog(id);

        return RespBean.ok("成功删除[" + count + "]条");
    }

}
